#include "AnalyControlBar.h"

AnalyControlBar::AnalyControlBar(wxWindow* parent,ImageDisplay* display) :
    wxPanel(parent,wxID_ANY),
    imageDisplay(display)
{
    listHandleImage=new wxListCtrl(this,(int)AnalyControlBarID::listHandleImage,wxDefaultPosition,wxDefaultSize,wxLC_REPORT);
    btnExeImageList=new wxButton(this,(int)AnalyControlBarID::btnExeImageList,wxT("处理图片序列"));
    spinAreaX=new wxSpinCtrl(this,(int)AnalyControlBarID::spinAreaX);
    spinAreaY=new wxSpinCtrl(this,(int)AnalyControlBarID::spinAreaY);
    btnFilter=new wxButton(this,(int)AnalyControlBarID::btnFilter,wxT("过滤图片"));
    spinThreshold=new wxSpinCtrl(this,(int)AnalyControlBarID::spinThreshold);
    btnExecute=new wxButton(this,(int)AnalyControlBarID::btnExecute,wxT("标记目标位置"));
    wxSizer* bsizer=new wxFlexGridSizer(2,5,5);
    wxSizer* asizer=new wxBoxSizer(wxVERTICAL);
    bsizer->Add(new wxStaticText(this,wxID_ANY,wxT("识别区域X")),wxSizerFlags(0).CenterVertical());
    bsizer->Add(spinAreaX,wxSizerFlags(0).Expand());
    bsizer->Add(new wxStaticText(this,wxID_ANY,wxT("识别区域Y")),wxSizerFlags(0).CenterVertical());
    bsizer->Add(spinAreaY,wxSizerFlags(0).Expand());
    bsizer->Add(new wxStaticText(this,wxID_ANY,wxT("过滤阈值")),wxSizerFlags(0).CenterVertical());
    bsizer->Add(spinThreshold,wxSizerFlags(0).Expand());
    asizer->Add(bsizer,wxSizerFlags(3).Expand());
    asizer->AddSpacer(5);
    asizer->Add(btnFilter,wxSizerFlags(1).Expand());
    asizer->Add(btnExecute,wxSizerFlags(1).Expand());
    asizer->Add(btnExeImageList,wxSizerFlags(1).Expand());
    asizer->AddSpacer(5);
    asizer->Add(listHandleImage,wxSizerFlags(6).Expand());
    SetSizer(asizer);

    spinThreshold->SetRange(0,254);
    spinAreaX->SetRange(0,10);
    spinAreaY->SetRange(0,10);

    Bind(wxEVT_LIST_ITEM_SELECTED,&AnalyControlBar::on_listHandleImage_select,this,(int)AnalyControlBarID::listHandleImage);
    Bind(wxEVT_SPINCTRL,&AnalyControlBar::on_spinHandleArea_change,this,(int)AnalyControlBarID::spinAreaX);
    Bind(wxEVT_SPINCTRL,&AnalyControlBar::on_spinHandleArea_change,this,(int)AnalyControlBarID::spinAreaY);
    Bind(wxEVT_BUTTON,&AnalyControlBar::on_btnFilter_click,this,(int)AnalyControlBarID::btnFilter);
    Bind(wxEVT_BUTTON,&AnalyControlBar::on_btnExecute_click,this,(int)AnalyControlBarID::btnExecute);
    Bind(wxEVT_BUTTON,&AnalyControlBar::on_btnExeImageList_click,this,(int)AnalyControlBarID::btnExeImageList);
}

AnalyControlBar::~AnalyControlBar()
{

}

void AnalyControlBar::SetImageData(unsigned char* imageData,long imageHeight,long imageWidth)
{
    handleImage.SetImage(imageData,imageHeight,imageWidth);
    imageDisplay->showBitmap(handleImage.GetDrawImage());
    spinAreaX->SetRange(0,imageWidth);
    spinAreaY->SetRange(0,imageHeight);
}

#include "MainFrame.h"
void AnalyControlBar::on_btnExeImageList_click(wxCommandEvent& event)
{
    MainFrame* mainPtr=(MainFrame*)GetParent();
    mainPtr->saveFile.SetFramePos(0);
    int32_t imageNum=mainPtr->saveFile.GetImageCount();
    wxPointList pList;
    listHandleImage->ClearAll();
    listHandleImage->AppendColumn(wxT("图像序号"));
    listHandleImage->AppendColumn(wxT("X_Pos"));
    listHandleImage->AppendColumn(wxT("Y_Pos"));
    listHandleImage->AppendColumn(wxT("时间戳"));
    long countNum=0;
    wxProgressDialog pdlg(wxT("正在处理"),wxT("正在处理数据"),imageNum,this);
    wxPoint* p;
    unsigned char* rawData;
    uint64_t timespan;
    for(int i=0;i<imageNum;i++)
    {
        if(!pdlg.Update(i))
        {
            mainPtr->saveFile.SetFramePos(mainPtr->sliderPlayControl->GetValue());  //再设置回去
            return;
        }
        
        mainPtr->saveFile.ReadImageWithTimestamp(rawData,timespan);
        try
        {
            p=new wxPoint(handleImage.GetGrayCenter(rawData,spinThreshold->GetValue()));
        }
        catch(const int e)
        {
            delete[] rawData;
            continue;
        }
        delete [] rawData;
        //添加新的列表元素
        pList.Append(p);
        //countNum=listHandleImage->InsertItem(countNum,wxString()<<i);
        wxListItem newItem;
        newItem.SetId(countNum);
        newItem.SetText(wxString()<<i);
        newItem.SetColumn(0);
        listHandleImage->InsertItem(newItem);
        newItem.SetText(wxString()<<p->x);
        newItem.SetColumn(1);
        listHandleImage->SetItem(newItem);
        newItem.SetText(wxString()<<p->y);
        newItem.SetColumn(2);
        listHandleImage->SetItem(newItem);
        newItem.SetText(wxString()<<timespan);
        newItem.SetColumn(3);
        listHandleImage->SetItem(newItem);
        countNum++;
    }
    handleImage.LinkPoints(pList);
    imageDisplay->showBitmap(handleImage.GetDrawImage());
    mainPtr->saveFile.SetFramePos(mainPtr->sliderPlayControl->GetValue());  //再设置回去
}

void AnalyControlBar::on_listHandleImage_select(wxListEvent& event)
{   
    int value=0;
    event.GetLabel().ToInt(&value);
    ((MainFrame*)GetParent())->emitSetPlayPos(value);
}

void AnalyControlBar::on_spinHandleArea_change(wxSpinEvent& event)
{
    handleImage.SetHandleArea(spinAreaX->GetValue(),spinAreaY->GetValue());
    imageDisplay->showBitmap(handleImage.GetDrawImage());
}

void AnalyControlBar::on_btnFilter_click(wxCommandEvent& event)
{
    handleImage.FilterImage(spinThreshold->GetValue());
    imageDisplay->showBitmap(handleImage.GetDrawImage());
}

void AnalyControlBar::on_btnExecute_click(wxCommandEvent& event)
{
    handleImage.HandleImage(spinThreshold->GetValue());
    imageDisplay->showBitmap(handleImage.GetDrawImage());
}

void AnalyControlBar::SetHandleArea(const wxSize& size)
{
    handleImage.SetHandleArea(size.GetX(),size.GetY());
    spinAreaX->SetValue(size.GetX());
    spinAreaY->SetValue(size.GetY());
}